package com.lewis.module.m2.api.bill.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lewis.common.core.entity.BaseResult;
import com.lewis.common.core.entity.controller.BaseController;
import com.lewis.common.core.entity.page.PageResult;
import com.lewis.common.core.utils.StringUtil;
import com.lewis.module.m2.api.bill.code.entity.BillCode;
import com.lewis.module.m2.api.bill.code.service.IBillCodeService;
import com.lewis.module.m2.api.bill.code.vo.BillCodeDeleteVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 账单编码Controller
 *
 * @author lewis
 * @since 2021-11-09
 */
@Api(tags = "账单")
@Slf4j
@RestController
@RequestMapping("/bill/code")
public class BillCodeController extends BaseController {

	@Resource
	private IBillCodeService billCodeService;

	/**
	 * 查询账单编码列表
	 */
	@GetMapping("/listLewis")
	public BaseResult listLewis(BillCode billCode) {

		StringBuilder msg = new StringBuilder();

		List<BillCode> billCodes = billCodeService.list(
				new LambdaQueryWrapper<BillCode>()
						.eq(StringUtil.isNotNull(billCode.getUserId()), BillCode::getUserId, billCode.getUserId())
						.eq(StringUtil.isNotNull(billCode.getBillName()), BillCode::getBillName, billCode.getBillName())
						.eq(BillCode::getIsDelete, billCode.getIsDelete())
		);
		if (billCodes.size() == 0) {
			return BaseResult.fail().setData("名下不存在账单");
		}

		for (BillCode code : billCodes) {
			msg.append("[").append(code.getId()).append("] ").append(code.getBillName()).append("\n");
		}
		return BaseResult.ok().setData(msg);
	}

	/**
	 * 新增账单编码
	 */
	@PostMapping("/add")
	public BaseResult add(@RequestBody BillCode billCode) {
		billCode.setIsDelete(0);
		StringBuilder msg = new StringBuilder();
		if (billCodeService.save(billCode)) {
			msg.append("创建完毕，要保持记账的好习惯哦~");
			return BaseResult.ok().setData(msg);
		}
		return BaseResult.fail().setData("创建失败，请重试。");
	}

	/**
	 * 删除账单编码
	 */
	@PostMapping("/delete")
	public BaseResult delete(@RequestBody BillCodeDeleteVo vo) {

		StringBuilder msg = new StringBuilder();

		LambdaQueryWrapper<BillCode> lqw = new LambdaQueryWrapper<BillCode>()
				.eq(BillCode::getId, vo.getId())
				.eq(BillCode::getUserId, vo.getUserId());

		BillCode billCode = billCodeService.getOne(lqw);
		if (billCode == null) {
			return BaseResult.fail().setData("禁止删除他人账单");
		}

		billCode.setIsDelete(1);
		if (billCodeService.updateById(billCode)) {
			msg.append("删除完毕，要保持记账的好习惯哦~");
			return BaseResult.ok().setData(msg);
		}

		return BaseResult.fail().setData("删除失败，请重试。");
	}

	@ApiOperation(value = "查询账单编码列表", notes = "")
	@GetMapping("/list")
	public PageResult list(BillCode billCode) {
		return billCodeService.selectBillCodeList(billCode);
	}

	@ApiOperation(value = "获取账单编码详细信息", notes = "")
	@GetMapping(value = "/{id}")
	public BaseResult getInfo(@PathVariable("id") Long id) {
		return ok().setData(billCodeService.getById(id));
	}

	@ApiOperation(value = "修改账单编码", notes = "")
	@PutMapping
	public BaseResult edit(@RequestBody BillCode billCode) {
		return getResult(billCodeService.updateById(billCode));
	}

	@ApiOperation(value = "删除账单编码", notes = "")
	@DeleteMapping("/{ids}")
	public BaseResult remove(@PathVariable Long[] ids) {
		return getResult(billCodeService.removeByIds(Arrays.asList(ids)));
	}
}